package com.sdry.web.controller.zm;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sdry.model.zm.ZmAgentEntity;
import com.sdry.model.zm.ZmGeneralQueryEntity;
import com.sdry.service.zm.ZmAgentService;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.zm.MethodLog;

@Controller
@RequestMapping("agent")
public class ZmAgentController {

	@Resource
	ZmAgentService zmAgentService;

	@RequestMapping("toAgent")
	public ModelAndView toAgent() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("zm/agentInfo");
		return mav;
	}

	@ResponseBody
	@RequestMapping("addAgent")
	public String addAgent(ZmAgentEntity agentEntity) {
		if (zmAgentService.addAgent(agentEntity)) {
			return "1";
		}
		return "0";
	}
	
	
	/**
	 * 这么写是可以拿到数据的
	 * 要么用@ResponseBody 要么用HttpResponse
	 */
/*	@ResponseBody
	@RequestMapping("delAgent")
	public String delAgent(@RequestParam(value = "agentId")Long agentId) {
		Long agentIdl = null;
		if(agentId!=null){
			agentIdl = (Long)agentId;			
		}
		if (zmAgentService.delAgent(agentIdl)) {
			return "1";
		}
		return "0";
	}*/
	/**
	 * 也可以直接用这样拿到参数
	 * @param agentId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delAgent")
	public String delAgent(Long agentId) {
		Long agentIdl = null;
		if(agentId!=null){
			agentIdl = (Long)agentId;			
		}
		if (zmAgentService.delAgent(agentIdl)) {
			return "1";
		}
		return "0";
	}
	
	@RequestMapping("editAgent")
	public String editAgent(HashMap<String, String> map) {
		if (zmAgentService.editAgent(map)) {
			return "1";
		}
		return "0";
	}
	@MethodLog(remarks="获取代理商列表")
	@RequestMapping("getAgentList")
	public void getList(HttpServletResponse response, ZmGeneralQueryEntity queryEntity) {
		if (queryEntity.getStrWhere() == null) {
			queryEntity.setStrWhere("status = 0 and isdel = 0 and ");
		}
		List<ZmAgentEntity> getAgentList = zmAgentService.getAgentList(queryEntity);
		int count = zmAgentService.countAgentList(queryEntity);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(getAgentList,
				jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
